//index.js
//获取应用实例
const app = getApp();
Page({
    data: {

        serviceTypeHead: [],
        typeId: '',//当前服务类型id
        navList: [
            {name: '三轮车', img: '/assets/images/index2.png', path: 'Tricycle'},
            {name: '套餐', img: '/assets/images/index3.png', path: 'SetMeal'},
            {name: '我的邀请', img: '/assets/images/index1.png', path: 'Recommend'}
        ],
        showLeft: false,
        animationData: {},
        showNav: false,
        animationDatas: {},
        ji_list: [],//等级列表
        border: ['', '', ''],
        showJi: false,
        levelId: 0,
        workers: 0,//听单人数
        formData: {
            days: '',
            addres: '',
            square: ''
        }
    },

    changeType (e){
        
        this.setData({
            typeId: e.detail.id
        })
        
    },

    onLoad: function (options) {

       
        if(typeof options.user_id != 'undefined') {
            // 受邀人
            wx.setStorageSync('inviter', options.user_id);
        }
        // 用户信息
        var userInfo = app.globalData.userInfo;
        if(!userInfo) {
            this.getUserInfo();
        } else {
            this.setData({
                userInfo: userInfo
            })
        }
        // 服务类型列表
        var typeList = app.globalData.serviceTypeList;
        
        if(!typeList) {
            this.getServiceTypeList();
        } else {
            var typeId = this.data.typeId;
            if(!typeId) {
                typeId = typeList[0]['id'];
            }
            this.setData({
                serviceTypeList: typeList,
                serviceTypeHead: typeList,
                typeId: typeId
            })
        }
        
        // 听单工人数量
        this.getWorkerOnline();

    },
    onReady: function() {

    },
    onHide: function () {
        this.setData({
            showLeft: false,
            showNav: false
        });
    },

    // 创建订单（呼叫师傅）
    orderAdd: function () {
        var data = {
            type_id: this.data.typeId,
            size: this.data.formData.square,
            address: this.data.formData.addres,
            lng: this.data.longitude,
            lat: this.data.latitude,
            duration: this.data.formData.days,
            level_id: this.data.levelId
        }

        var object = {
            url: app.api.url.order.orderAdd,
            data: data,
        }

        // 用户信息验证
        if(!app.globalData.userInfo.phone){
            app.navigateTo(app.page.PerfectInformation)
            return;
        }
        
        app.request(object).then(res =>{

            if(res.status == 1) {
                app.redirectTo(app.page.SingleState, 'id='+res.data.order_id);
            } else {
                app.showToast(res.info);
            }
            
        })
    },

    toPage: function (e) {
        let page = e.currentTarget.dataset.page;
        app.navigateTo(app.page[page]);
    },
    
    //侧边导航菜单
    onGetCode: function (e) {
      
        let that = this;
        let animation = wx.createAnimation({
            duration: 400,
            timingFunction: 'linear'
        })
        that.animation = animation
        animation.translateX(-390).step();
        that.setData({
            animationData: animation.export(),
            showLeft: e.detail,
            showNav: false
        });
        setTimeout(function () {
            animation.translateX(0).step()
            that.setData({
                animationData: animation.export(),
            })
        }, 200)
    },
    onNav: function (e) {
        let that = this;
        let animation = wx.createAnimation({
            duration: 200,
            timingFunction: 'linear'
        })
        that.animation = animation
        animation.translateY(-390).step();
        that.setData({
            animationDatas: animation.export(),
            showNav: e.detail
        });
        setTimeout(function () {
            animation.translateY(0).step()
            that.setData({
                animationDatas: animation.export(),
            })
        }, 200)
    },

    /**
     * 监听
     */
    inputChance: function (even) {
        
        let $dataset = even.currentTarget.dataset;
        this.data.formData[$dataset.type] = even.detail.value;
        
        this.setData({
            formData: this.data.formData
        })

        if (this.data.formData.days !== '' && this.data.formData.addres !== '' && this.data.formData.square !== '') {
            // 调接口查看价格
            var _this = this;
            var data = {size: this.data.formData.square};
            var object = {
                url: app.api.url.other.calculator,
                data: data,
            }
            app.request(object).then(res =>{

                if(res.status == 1) {
                    _this.setData({
                        ji_list: res.data,
                        showJi: true
                    })
                } else {
                    app.showToast(res.info);
                }
            })

        } else {

            this.setData({
                showJi: false
            })

        }
    },

    // 选择位置
    getLocation: function(e) {
        var _this = this;

        const success = (res) =>{

            var formData = _this.data.formData;
                formData.addres = res.address;
                
                _this.setData({
                    addres: res.address,
                    latitude: res.latitude,
                    longitude: res.longitude,
                    formData: formData
                })
            
        };

        const fail = (error) =>{
            console.log(error)
        }
        
        app.wxmp.getSetting().then(res =>{

            if(!res.authSetting['scope.userLocation']){
                app.showToast('请授权获取您的位置')
            }

        })
        
        app.wxmp.chooseLocation().then(success).catch()
    },

    /**
     * 获取用户信息
     */
    getUserInfo: function(){
        var _this = this;
        var object = {
            url: app.api.url.user.userInfo,
            success: function(res){
                if(res.status == 1) {
                    app.globalData.userInfo = res.data;
                    _this.setData({
                        userInfo: res.data
                    })

                } else {
                    app.showToast(res.info);
                }
            },
            complete: function() {
                wx.stopPullDownRefresh() //停止下拉刷新
            }
        }
        app.request(object);
    },
    /**
     * 获取服务类型列表
     */
    getServiceTypeList: function(){
        var _this = this;
        var object = {
            url: app.api.url.other.typeList,
            success: function(res) {
                if(res.status == 1) {
                    app.globalData.serviceTypeList = res.data;
                    
                    if(res.data.length > 0) {
                        var typeId = _this.data.typeId;
                        if(!typeId) {
                            typeId = res.data[0]['id'];
                        }

                        _this.setData({
                            serviceTypeList: res.data,
                            serviceTypeHead: res.data,
                            typeId: typeId
                        })
                    }
                } else {
                    app.showToast(res.info);
                }
            }
        }
        app.request(object);
    },
    /**
     * 获取等级列表
     */
    getWorkerOnline: function(){
        var _this = this;
        var object = {
            url: app.api.url.other.workerOnline,
            success: function(res) {
                if(res.status == 1) {
                    _this.setData({
                        workers: res.data.num,
                    })
                } else {
                    app.showToast(res.info);
                }
            }
        }
        app.request(object);
    },

    changeLevel: function (e) {
        var index = e.currentTarget.dataset.item.id;
        var border = ['', '', ''];
        border[index-1] = 'box-shadow: 0 10rpx 16rpx -10rpx rgba(255,198,112,.5); border:solid 1px #fcae29;';
        this.setData({
            levelId: index,
            border: border
        })
    },


});
